home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2612 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  129 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can be C a so strange language ???. Problem with a function returning a pointer...
  5. Date: Mon, 22 Jan 96 15:33:07 GMT
  6. Organization: none
  7. Message-ID: <822324787snz@genesis.demon.co.uk>
  8. References: <4e015j$drt@hermes.fundp.ac.be>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4e015j$drt@hermes.fundp.ac.be>
  15.            fmelo@biq.fundp.ac.be "Francisco Melo Ledermann" writes:
  16.  
  17. >Hi everybody !, I have a problem that for me appears to a really strange 
  18. >one. I have a function that call another function. The function 
  19. >that is called is the next one:
  20. >
  21. >struct heavy_atom *Set_Position_AA_Number (int residue_number, 
  22. >                                        struct heavy_atom *position_ptr)
  23. >{
  24. > int out;
  25. > out = 0;
  26. >
  27. > while (out != 1)
  28. > {
  29. >  if ((*position_ptr).res_number != residue_number)
  30. >  {
  31. >   if ((*position_ptr).cooh_ptr != NULL)
  32.  
  33. The normal more readable way of writing this in C is:
  34.  
  35.     if (position_ptr->cooh_ptr != NULL)
  36.  
  37. >   {
  38. >    position_ptr = (*position_ptr).cooh_ptr;
  39. >   }
  40. >  }
  41. >   else
  42. >   {
  43. >    return (position_ptr);
  44. >    out = 1;
  45. >   }
  46. > }
  47. >}   /* end of Set_Position_AA_Number function */
  48.  
  49. This looks rather convoluted and appears to go into an infinite loop if it
  50. hits the end of the list. Maybe you wanted something like:
  51.  
  52. {
  53.     while (position_ptr != NULL && position_ptr->res_number != residue_number)
  54.         position_ptr = position_ptr->cooh_ptr;
  55.  
  56.     return position_ptr;
  57. }
  58.  
  59. This returns a null pointer if the end of the list is reached without
  60. a match. It also copes with being passed an empty list.
  61.  
  62. >The other function that contains the call is the next one:
  63. >
  64. >(void) Function_Caller ()
  65.  
  66. I assume you mean
  67.  
  68. void Function_Caller ()
  69.  
  70. >{
  71. >int counter;
  72. >
  73. >struct heavy_atom *first_atom,
  74. >                  *actual_atom;
  75. >
  76. >..
  77. >..
  78. >
  79. >actual_atom = Set_Position_AA_Number (counter, first_atom);
  80. >
  81. >..
  82. >..
  83. >
  84. >}   /* end of Function_Caller */
  85. >
  86. >
  87. >The problem is if I debbug and check the current values just in the line 
  88. >before call the function I have:
  89. >
  90. >counter = 1
  91. >(*first_atom).res_number = 1
  92. >
  93. >but the function receives:
  94. >
  95. >counter = 0
  96. >(*first_atom).res_number = 1
  97.  
  98. Most likely this is because you have called Set_Position_AA_Number()
  99. without a declaration for it in scope. Without a declaration the compiler
  100. would have assumed that it returned int and then generated the wrong
  101. call sequence. Make sure you have a line such as:
  102.  
  103. struct heavy_atom *Set_Position_AA_Number (int residue_number, 
  104.                                            struct heavy_atom *position_ptr);
  105.  
  106. before Function_Caller();
  107.  
  108. >I am really surprised about that. I am not a C expert programmer and this 
  109. >is the first time that I construct a function that returns a pointer, and 
  110. >I am sure that the problem is going in that way, because the same 
  111. >function works for me without return a pointer. What's going on here ?. 
  112. >For me this is a really strange thing about C. The FAQ doesn't cover 
  113. >about functions returning pointers (and about funcions in general as a 
  114. >special section) and for that reason I am asking here. If you know what 
  115. >is happening would be great for me !. In advance, thanks a lot for your 
  116. >time and help, best wishes,
  117.  
  118. There's nothing really special about functions returning pointers, so long
  119. as the return value doesn't point to a local variable in the funciton
  120. (which won't exist after the function has returned). This isn't a problem
  121. with your code however.
  122.  
  123. -- 
  124. -----------------------------------------
  125. Lawrence Kirby | fred@genesis.demon.co.uk
  126. Wilts, England | 70734.126@compuserve.com
  127. -----------------------------------------
  128.